package com.android.volleydemo;



import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.android.volley.wrapper.VolleyEncapsulation.IVolleyEncapsulationListener;
import com.android.volley.wrapper.VolleyEncapsulation.VolleyErrorType;
import com.volley.help.LruMemoryCache;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements
		IVolleyEncapsulationListener {
	private GridView mGridView;
	private String[] urls;
	private LruMemoryCache mCache;
	private ImageLoader loader;
	private RequestQueue queue;
	private int itemWidth;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		urls = Constants.IMAGES;
		mGridView = (GridView) findViewById(R.id.gridView);
		mCache = new LruMemoryCache(1000);
		queue = Volley.newRequestQueue(MainActivity.this);
		loader = new ImageLoader(queue, new ImageLoader.ImageCache() {

			@Override
			public void putBitmap(String url, Bitmap bitmap) {
				mCache.putBitmap(url, bitmap);
			}

			@Override
			public Bitmap getBitmap(String url) {
				return mCache.getBitmap(url);
			}
		});
		// DisplayMetrics dm = new DisplayMetrics();
		// getWindowManager().getDefaultDisplay().getMetrics(dm);
		// Context context = this;
		// // 7.0f为GridView's item horizontalSpace
		// // 16.0f为GridView paddingLeft+paddingRight
		// int screenWidth = dm.widthPixels;
		// int mMaxFloorNum = 3;
		// float padding = dip2px(context, 16.0f);
		// float horizontalSpace = dip2px(context, 7.0f);
		// itemWidth = (int) ((screenWidth - padding
		// - mGridView.getHorizontalFadingEdgeLength() - horizontalSpace * 2) /
		// 3.0f);
		// int allWidth = (int) ((itemWidth) * mMaxFloorNum + padding +
		// horizontalSpace * 2);
		// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
		// allWidth, LinearLayout.LayoutParams.FILL_PARENT);
		// System.out.println("#allWidth:" + allWidth + "#itemWidth:" +
		// itemWidth);
		// System.out.println("#screenWidth:" + screenWidth);
		// System.out.println("#" + mGridView.getHorizontalFadingEdgeLength());
		// mGridView.setLayoutParams(params);
		// mGridView.setColumnWidth(itemWidth);
		// mGridView.setStretchMode(GridView.NO_STRETCH);
		// mGridView.setNumColumns(mMaxFloorNum);
		fillGridViewWidth(this, mGridView, 2);
		mGridView.setAdapter(new GridAdapter());
		test();
	}

	private void test() {
//		int requestId = 0x0001;
//		VolleySocketEncapsulation mSocketEncapsulation = new VolleySocketEncapsulation(
//				Global.CASE_BASE_DESC, true);
//		VolleyEncapsulation mEncapsulation = new VolleyEncapsulation(this,
//				mSocketEncapsulation, Request.Method.GET);
//		mEncapsulation.setIVolleyEncapsulationListener(this);
//		mEncapsulation.postVolleyParam(requestId);
		System.out.println("##"+dip2px(this, 10));
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static void fillGridViewWidth(Context context, GridView gridView,
			int columnNum) {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		// 7.0f为GridView's item horizontalSpace
		// 16.0f为GridView paddingLeft+paddingRight
		int screenWidth = dm.widthPixels;
		float padding = dip2px(context, 16.0f);
		padding = gridView.getPaddingLeft() + gridView.getPaddingRight();
		float horizontalSpace = dip2px(context, 8.0f);
		int itemWidth = (int) ((screenWidth - padding - horizontalSpace
				* (columnNum - 1)) / (columnNum * 1.0f));
		int allWidth = (int) (itemWidth * columnNum + padding + horizontalSpace
				* (columnNum - 1));
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				allWidth, LinearLayout.LayoutParams.FILL_PARENT);
		System.out.println("#allWidth:" + allWidth + "#itemWidth:" + itemWidth);
		System.out.println("#screenWidth:" + screenWidth);
		System.out.println("#" + gridView.getHorizontalFadingEdgeLength());
		gridView.setLayoutParams(params);
		gridView.setColumnWidth(itemWidth);
		gridView.setStretchMode(GridView.NO_STRETCH);
		gridView.setNumColumns(columnNum);
	}

	private class GridAdapter extends BaseAdapter {
		private final String TAG = GridAdapter.class.getSimpleName();

		@Override
		public int getCount() {
			return urls.length;
		}

		@Override
		public Object getItem(int position) {
			return urls[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// mCache.checkMemory();
			convertView = getLayoutInflater().inflate(R.layout.grid_item, null);
			final ImageView img = (ImageView) convertView
					.findViewById(R.id.img);
			String url = urls[position];
			ImageListener imageListener = new ImageListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					// 异常图片
					img.setImageResource(android.R.drawable.ic_delete);
					Log.d(TAG, "#error:" + error.toString());
				}

				@Override
				public void onResponse(ImageContainer response,
						boolean isImmediate) {
					if (response.getBitmap() != null) {
						img.setImageBitmap(response.getBitmap());
					} else {
						// img.setImageResource(android.R.drawable.ic_delete);
					}
				}
			};
			loader.get(url, imageListener);
			return convertView;
		}

	}

	@Override
	public void onVolleyResponseError(VolleyErrorType arg0, String arg1,
			int arg2) {
		System.out.println("#onVolleyResponseError:"+arg1);

	}

	public void onVolleyResponseSuccess(com.alibaba.fastjson.JSONObject arg0,
			int arg1) {
		System.out.println("#onVolleyResponseSuccess:"+arg0.toString());
	};
}
